home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / go / prog / nextgo23.taz / nextgo23 / NeXTGo / matchpat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-09  |  3.9 KB  |  164 lines

  1. #include "comment.header"
  2.  
  3. #define EMPTY 0
  4. #define MAXPC 16
  5. #define abs(x) ((x) < 0 ? -(x) : (x))
  6. #define line(x) (abs(x - 9))
  7.  
  8. extern unsigned char p[19][19];
  9. extern int currentStone, opposingStone, MAXX, MAXY;
  10. extern int lib;
  11. extern void countlib(int,int,int);
  12.  
  13. int matchpat(int m, int n, int *i, int *j, int *val)
  14.      /* match pattern and get next move */
  15. {
  16.   struct patval {int x, y, att;}; /* pattern x, y coor and attribute */
  17.   /* att = 0 - empty, 1 - your piece, 2 - my piece, 3 - my next move */
  18.   /* 4 - empty on edge, 5 - your piece on edge, 6 - my piece on edge */
  19.   struct pattern {
  20.     struct patval patn[MAXPC];   /* pattern */
  21.     /* number of pieces in pattern, no. of transformation, pattern value */
  22.     int patlen, trfno, patwt;
  23.   };
  24.   
  25. #include "patterns.h"
  26.   
  27.   /* transformation matrice */
  28.   static int trf [8][2][2] = {
  29.     {{1, 0}, {0, 1}}, /* linear transfomation matrix */
  30.     {{1, 0}, {0, -1}},  /* invert */
  31.     {{0, 1}, {-1, 0}},  /* rotate 90 */
  32.     {{0, -1}, {-1, 0}},    /* rotate 90 and invert */
  33.     {{-1, 0}, {0, 1}},  /* flip left */
  34.     {{-1, 0}, {0, -1}},    /* flip left and invert */
  35.     {{0, 1}, {1, 0}},  /* rotate 90 and flip left */
  36.     {{0, -1}, {1, 0}}  /* rotate 90, flip left and invert */
  37.   };
  38.   int k, my, nx, l, r, cont;
  39.   int ti, tj, tval;
  40.   
  41.   *i = -1;   *j = -1;   *val = -1;
  42.   ti = tj = 0;
  43.   for (r = 0; r < PATNO; r++)
  44.     /* try each pattern */
  45.     for (l = 0; l < pat[r].trfno; l++)
  46.       /* try each orientation transformation */
  47.       {
  48.     k = 0;  cont = 1;
  49.     while ((k != pat[r].patlen) && cont)
  50.       /* match each point */
  51.       {
  52.         /* transform pattern real coordinate */
  53.         nx = n + trf[l][0][0] * pat[r].patn[k].x
  54.           + trf[l][0][1] * pat[r].patn[k].y;
  55.         my = m + trf[l][1][0] * pat[r].patn[k].x
  56.           + trf[l][1][1] * pat[r].patn[k].y;
  57.         
  58.         /* outside the board */
  59.         if ((my < 0) || ( my > MAXY - 1) || (nx < 0) || (nx > MAXX - 1))
  60.           {
  61.         cont = 0;
  62.         break;
  63.           }
  64.         switch (pat[r].patn[k].att) {
  65.         case 0 : if (p[my][nx] == EMPTY)    /* open */
  66.           break;
  67.         else
  68.           {
  69.         cont = 0;
  70.         break;
  71.           }
  72.         case 1 : if (p[my][nx] == opposingStone)  /* your piece */
  73.           break;
  74.         else
  75.           {
  76.         cont = 0;
  77.         break;
  78.           }
  79.         case 2 : if (p[my][nx] == currentStone)  /* my piece */
  80.           break;
  81.         else
  82.           {
  83.         cont = 0;
  84.         break;
  85.           }
  86.         case 3 : if (p[my][nx] == EMPTY)    /* open for new move */
  87.           {
  88.         lib = 0;
  89.         countlib(my, nx, currentStone);    /* check liberty */
  90.         if (lib > 1)  /* move o.k. */
  91.           {
  92.             ti = my;
  93.             tj = nx;
  94.             break;
  95.           }
  96.         else
  97.           {
  98.             cont = 0;
  99.             break;
  100.           }
  101.           }
  102.         else
  103.           {
  104.         cont = 0;
  105.         break;
  106.           }
  107.         case 4 : if ((p[my][nx] == EMPTY)  /* open on edge */
  108.              && ((my == 0) || (my == MAXY - 1) || (nx == 0) || (nx == MAXX - 1)))
  109.           break;
  110.         else
  111.           {
  112.         cont = 0;
  113.         break;
  114.           }
  115.         case 5 : if ((p[my][nx] == opposingStone)  /* your piece on edge */
  116.              && ((my == 0) || (my == MAXY - 1) || (nx == 0) || (nx == MAXX - 1)))
  117.           break;
  118.         else
  119.           {
  120.         cont = 0;
  121.         break;
  122.           }
  123.         case 6 : if ((p[my][nx] == currentStone)  /* my piece on edge */
  124.              && ((my == 0) || (my == MAXY - 1) || (nx == 0) || (nx == MAXX - 1)))
  125.           break;
  126.         else
  127.           {
  128.         cont = 0;
  129.         break;
  130.           }
  131.         }
  132.         ++k;
  133.       }
  134.     if (cont)   /* match pattern */
  135.       {
  136.         tval = pat[r].patwt;
  137.         if ((r >= 8) && (r <= 13))    /* patterns for expand region */
  138.           {
  139.         if (line(ti) > 7)  /* penalty on line 1, 2 */
  140.           tval--;
  141.         else
  142.           if ((line(ti) == 6) || (line(ti) == 7))
  143.             tval++;    /* reward on line 3, 4 */
  144.         
  145.         if (line(tj) > 7)  /* penalty on line 1, 2 */
  146.           tval--;
  147.         else
  148.           if ((line(tj) == 6) || (line(tj) == 7))
  149.             tval++;    /* reward on line 3, 4 */
  150.           }
  151.         if (tval > *val)
  152.           {
  153.         *val = tval;
  154.         *i = ti;
  155.         *j = tj;
  156.           }
  157.       }
  158.       }
  159.   if (*val > 0)    /* pattern matched */
  160.     return 1;
  161.   else  /* match failed */
  162.     return 0;
  163. }  /* end matchpat */
  164.